<?php

/**
 * Framework_Util_XmlRpcClient
 *
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
 * @filesource
*/


require_once 'XML/RPC.php';

class Framework_Util_XmlRpcClient {

	private $_xmlRpcServerHost ;
	private $_xmlRpcServerPath ;
	private $_xmlRpcClient ;
	private $_username ;
	private $_password ;

	public function __construct(
			$host = XML_RPC_SERVER_HOST, 
			$path = XML_RPC_SERVER_PATH, 
			$user = XML_RPC_USERNAME, 
			$pass = XML_RPC_PASSWORD) {
	
		if( empty( $host ) || empty( $path )) {
			$error = Framework_ErrorHandler::raiseError( "No XML-RPC host and/or path defined.", __METHOD__."::".__LINE__ ) ;
			die( $error->getMessage() ) ;
		} else {
			$this->_xmlRpcClient = new XML_RPC_Client( $path, $host ) ;
		}

		$this->_username = $user ;
		$this->_password = $pass ;
	
	}// end constructor


	
	public function __destruct() {}


	
	public function sendCall($xmlRpcCall, $params = null, $authenticate = false, $debug = false) {
		
		if ($debug) {
			$this->_xmlRpcClient->setDebug(1);
		}
		
		if ($authenticate) {
			if (empty($this->_username) || empty($this->_password)) {
				$error = Framework_ErrorHandler::raiseError( "XML-RPC authentication requested, but no username/password assigned to object.") ;
				die( $error->getMessage() ) ;
			} else {
				$this->_xmlRpcClient->setCredentials( $this->_username, $this->_password ) ;
			}
		}

		if (is_null($params)) {
			$params = new XML_RPC_Value() ;
		}
		
		// send the message
		$msg = new XML_RPC_Message($xmlRpcCall, $params) ;


		$response = $this->_xmlRpcClient->send($msg) ;
		
		// handle the response
		if (!$response->faultCode()) {
			$_output = XML_RPC_decode( $response->value() ) ;
		} else {
			$_output = Framework_ErrorHandler::raiseError( "XML-RPC Fault. [code = ".$response->faultCode().", reason = ".$response->faultString()) ;
		}
		
		return $_output ;

	}// end sendCall




}// end class


?>
